﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Assets;

namespace Marbles.Web.Admin.Content
{
    public partial class FileMgr : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            AssetCollection items = new AssetCollection(this.CurrentWeb.SiteId);
            cLstFiles.DataSource = items;
            cLstFiles.DataBind();
        }


        protected void Page_Command(object sender, CommandEventArgs e)
        {
            Asset asset = null;

            switch (e.CommandName)
            {
                case "New":
                    cDlgFileUpload.Title = "Create new File";
                    cDlgFileUpload.Visible = true;
                    cBtnSave.CommandName = "Create";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cBtnView.CommandArgument = "";
                    cBtnView.Visible = false;
                    cBtnDelete.Visible = false;
                    cLblLastAccess.Text = "";
                    break;
                case "Create":
                    asset = Asset.CreateNew(this.CurrentWeb);
                    asset.Upload(cFileUpload);
                    asset.Insert();
                    this.cDlgFileUpload.Visible = false;
                    LoadData();
                    break;
                case "Modify":
                    asset = new Asset(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    cDlgFileUpload.Title = "Modify Existing File";
                    cBtnSave.CommandName = "Update";
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnView.CommandArgument = e.CommandArgument.ToString();
                    cBtnDelete.CommandArgument = e.CommandArgument.ToString();
                    cLblLastAccess.Text = asset.LastAccessDate;
                    cBtnView.Visible = true;
                    cBtnDelete.Visible = true;
                    cDlgFileUpload.Visible = true;
                    cBtnView.OnClientClick = "DownloadAsset('" + e.CommandArgument.ToString() + "');return false;";
                    break;
                case "Cancel":
                    this.cDlgFileUpload.Visible = false;
                    cBtnSave.CommandName = "Create";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cBtnView.CommandArgument = "";
                    break;
                case "Update":
                    asset = new Asset(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    asset.Upload(cFileUpload);
                    asset.Update();
                    cBtnSave.CommandName = "Create";
                    cBtnSave.CommandArgument = "";
                    cBtnDelete.CommandArgument = "";
                    cBtnView.CommandArgument = "";
                    cDlgFileUpload.Visible = false;
                    LoadData();
                    break;
                case "Delete":
                    asset = new Asset(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    asset.Delete();
                    cDlgFileUpload.Visible = false;
                    LoadData();
                    break;
            }
        }
    }
}